import UIKit
import AssetsLibrary

let IOS9_OR_LATER = UIDevice.currentDevice().systemVersion.compare("9.0") != NSComparisonResult.OrderedAscending

class AlbumImage: NSObject {
    lazy var representation: ALAssetRepresentation? = {
        return self.asset.defaultRepresentation()
    }()
    
    // 格式类型
    lazy var UTI: String = {
        if let representation = self.representation {
            return representation.filename()
        }
        return ""
    }()
    
    // 图片名
    lazy var filename: String = {
        if let representation = self.representation {
            return representation.UTI()
        }
        return ""
    }()
    
    // 文件url
    lazy var url: NSURL? = {
        if let representation = self.representation {
            return representation.url()
        }
        return nil
    }()
    
    // 小图片
    lazy var thumbnail: UIImage? = {
        // ios9 以后thumbnail显示很模糊
        let image = IOS9_OR_LATER ? self.asset.aspectRatioThumbnail().takeUnretainedValue() : self.asset.thumbnail().takeUnretainedValue()
        return UIImage(CGImage: image)
    }()
    
    // 压缩原图
    lazy var compressionImage: UIImage? = {
        let fullScreenImage = UIImage(CGImage: self.asset.defaultRepresentation().fullScreenImage().takeUnretainedValue())
        if let data = UIImageJPEGRepresentation(fullScreenImage, 0.1) {
            return UIImage(data: data)
        }
        return nil
    }()
    
    // 模糊原图
    lazy var originImage: UIImage? = {
        return UIImage(CGImage: self.asset.defaultRepresentation().fullScreenImage().takeUnretainedValue())
    }()
    
    // 原图
    lazy var fullResolutionImage: UIImage? = {
        let rep = self.asset.defaultRepresentation()
        let iref = rep.fullResolutionImage().takeUnretainedValue()
        let orientation = UIImageOrientation(rawValue:rep.orientation().hashValue)!
        let fullResolutionImage = UIImage(CGImage: iref, scale: CGFloat(rep.scale()), orientation: orientation)
        return fullResolutionImage
    }()
    
    // 图片大小
    lazy var byteSize: Int64 = {
        if let representation = self.representation {
            return representation.size()
        }
        return 0
    }()
    
    // 尺寸大小
    lazy var dimensions: CGSize = {
        if let representation = self.representation {
            return representation.dimensions()
        }
        return CGSizeZero
    }()
    
    // 图片放大比例
    lazy var scale: Float = {
        if let representation = self.representation {
            return representation.scale()
        }
        return 0
    }()
    
    // 方向
    lazy var orientation: ALAssetOrientation = {
        if let representation = self.representation {
            return representation.orientation()
        }
        return .Up
    }()
    
    // 基本信息
    lazy var albumImageInfo: AlbumImageInfo? = {
        if let representation = self.representation {
            return AlbumImageInfo(dict: representation.metadata())
        }
        return nil
    }()
    
    var isSelected = false
    var selectedIndex = -1
    
    var asset: ALAsset
    init(asset: ALAsset) {
        self.asset = asset
        
        super.init()
        // print("AlbumImage: UTI:\(UTI), filename:\(filename), url:\(url), photo:\(thumbnail), byteSize:\(byteSize), dimensions:\(dimensions), scale:\(scale), orientation:\(orientation.rawValue)")
    }
    
    deinit {
        // print("Album -- AlbumImage: \(filename) deinit")
    }
}
